﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WinStudent
{
    public partial class FrmCourseList : Form
    {
        public FrmCourseList()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 初始化学期列表、所有课程列表信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmCourseList_Load(object sender, EventArgs e)
        {        
            InitCourseList();
            InitCnoList();         
        }

        private void InitCourseList()
        {
            string sql = "select Cno,Cname,Credit,Semester from Course where IsDeleted=0 ";
            DataTable datatableGradeList = SqlHelper.GetDataTable(sql);
            dgvCourseList.DataSource = datatableGradeList;
            FrmStudentList.Init_colEdit_colDelete(dgvCourseList);
        }

        private void InitCnoList()
        {
            string sql = "select Cno from Course order by Cno";
            DataTable datatableCnoList = SqlHelper.GetDataTable(sql);
            DataRow dataRow = datatableCnoList.NewRow();
            dataRow["Cno"] = "请选择";
            datatableCnoList.Rows.InsertAt(dataRow, 0);

            cboCno.DataSource = datatableCnoList;

            cboCno.DisplayMember = "Cno";//显示的内容
            cboCno.ValueMember = "Cno";//值

            cboCno.SelectedIndex = 0;
        }

        private void btnFind_Click(object sender, EventArgs e)
        {


            string cno = (string)cboCno.SelectedValue;
            string cname = txtCname.Text.Trim();

            string sql = "select Cno,Cname,Credit,Semester from Course where IsDeleted=0 and Cname like @Cname ";
            if (cboCno.SelectedIndex!=0)
            {
                sql += "and Cno=@Cno ";
            }
            SqlParameter[] paras =
            {
                new SqlParameter("@Cno",cno),
                new SqlParameter("@Cname","%"+cname+"%")
            };
            DataTable dataTableCourseList = SqlHelper.GetDataTable(sql, paras);

            dgvCourseList.DataSource = dataTableCourseList;
        }


        private void dgvCourseList_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1)
            {
                DataRow dr = (dgvCourseList.Rows[e.RowIndex].DataBoundItem as DataRowView).Row;

                DataGridViewCell cell = dgvCourseList.Rows[e.RowIndex].Cells[e.ColumnIndex];
                if (cell is DataGridViewLinkCell && cell.FormattedValue.ToString() == "修改")
                {
                    //修改操作
                    string Cno = dr["Cno"].ToString().Trim();
                    FrmEditCourse frmEditCourse = new FrmEditCourse();
                    frmEditCourse.Tag = Cno;
                    frmEditCourse.MdiParent = this.MdiParent;
                    frmEditCourse.Show();//顶级窗体
                }
                else if (cell is DataGridViewLinkCell && cell.FormattedValue.ToString() == "删除")
                {
                    //删除操作
                    if (MessageBox.Show("确定删除？", "删除课程提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        string cno = dr["Cno"].ToString().Trim();
                        //假删除 IsDeleted 0  1
                        string sqlDel = "update Course set IsDeleted=1 where Cno=@Cno ";
                        SqlParameter para = new SqlParameter("@Cno", cno);
                        int count = SqlHelper.ExecutNonQuery(sqlDel, para);

                        //SC表中的相关学生信息一同删除
                        string sqlDelSC = "update SC set IsDeleted=1 where Cno=@Cno";
                        SqlParameter paraDelSC = new SqlParameter("@Cno", cno);
                        SqlHelper.ExecuteScalar(sqlDelSC, paraDelSC);

                        if (count > 0)
                        {
                            MessageBox.Show("删除成功", "删除课程提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            //DataGridView数据并没有刷新，手动刷新
                            DataTable dtCourse = (DataTable)dgvCourseList.DataSource;
                            //dgvCourseList.DataSource = null;
                            dtCourse.Rows.Remove(dr);
                            dgvCourseList.DataSource = dtCourse;
                        }
                        else
                        {
                            MessageBox.Show("删除失败", "删除课程提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                        //真删除 delete...where... 
                    }
                }
            }
        }



    }
}
